<?php

class AuthController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function loginAction() {

        $request = $this->getRequest();
        $data = $request->getPost();


        $username = $data['member_code'];
        $password = $data['pin'];
        $authAdapter = $this->getAuthAdapter();

        $authAdapter->setIdentity($username)
                    ->setCredential($password);

        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);



        if($result->isValid()){
            $identity = $authAdapter->getResultRowObject();
            //print_r($identity);

            //$authStorage = $auth->getStorage();
            //$authStorage->write($identity);

            $sess = new Zend_Session_Namespace('Zend_Auth');
            $sess->data = $identity;
            $sess->setExpirationSeconds(6000);

            //echo $identity->member_code;
            //print_r($sess->data->member_code);
            //die();

            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "member");
        } else {
            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "index/login/msg/User Code atau PIN salah!");
        }

        //echo $username. " - ".$data['password']. "- ".$password;
        if ($authAdapter->isValid()) {
            $identity = $authAdapter->getIdentity();
            $authStorage = $auth->getStorage();
            $authStorage->write($identity);
            $sess = new Zend_Session_Namespace('Zend_Auth');
            $sess->setExpirationSeconds(6000);

            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "index/welcome");
            //echo "OK";
        } else {
            //echo "NOK";
            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "index/index/msg/".$authAdapter->getMessage() );
        }

         //$this->render('index');
    }


    public function loginadminAction() {

        $request = $this->getRequest();
        $data = $request->getPost();


        $username = $data['username'];
        $password = $data['pass'];
        $authAdapter = $this->getAdminAuthAdapter();

        $authAdapter->setIdentity($username)
                    ->setCredential($password);

        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);

        if($result->isValid()){
            $identity = $authAdapter->getResultRowObject();

            //$authStorage = $auth->getStorage();
            //$authStorage->write($identity);

            $sess = new Zend_Session_Namespace('Zend_Auth_Admin');
            $sess->data = $identity;
            $sess->setExpirationSeconds(6000);

            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "sn");
        } else {
            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "index/admin/msg/Username atau Password salah!");
        }

        //echo $username. " - ".$data['password']. "- ".$password;
        if ($authAdapter->isValid()) {
            $identity = $authAdapter->getIdentity();
            $authStorage = $auth->getStorage();
            $authStorage->write($identity);
            $sess = new Zend_Session_Namespace('Zend_Auth');
            $sess->setExpirationSeconds(6000);

            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "index/welcome");
            //echo "OK";
        } else {
            //echo "NOK";
            $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . "index/admin/msg/".$authAdapter->getMessage() );
        }

         //$this->render('index');
    }


    public function logoutAction() {
        //Zend_Auth::getInstance()->clearIdentity();
        Zend_Session::destroy(true);
        $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . 'index/index/msg/Anda berhasil keluar');
        //$this->_redirect('http://cbr.local');
    }

    public function logoutadminAction() {
        //Zend_Auth::getInstance()->clearIdentity();
        Zend_Session::destroy(true);
        $this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl() . 'index/admin/msg/Anda berhasil keluar');
    }

    private function getAuthAdapter() {
        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table_Abstract::getDefaultAdapter());
        $authAdapter->setTableName('members')
                ->setIdentityColumn('member_code')
                ->setCredentialColumn('pin');

        return $authAdapter;
    }

    private function getAdminAuthAdapter() {
        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table_Abstract::getDefaultAdapter());
        $authAdapter->setTableName('admin')
                ->setIdentityColumn('username')
                ->setCredentialColumn('pass');

        return $authAdapter;
    }


}

